<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="mini" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" placeholder="请选择" clearable
              @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label"
                :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="服务日期" prop="serviceDate">
            <el-date-picker type="month" clearable v-model="queryParams.serviceDate" value-format="yyyy-MM-dd"
              placeholder="请选择服务日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="开票状态" prop="invoiceStatus">
            <el-select v-model="queryParams.invoiceStatus" placeholder="请选择">
              <el-option v-for="item in invoiceStatusOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="签收状态" prop="signStatus">
            <el-select v-model="queryParams.signStatus" placeholder="请选择">
              <el-option v-for="item in signStatusOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['cld:invoice:export']">列表导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="invoiceList" border>
      <el-table-column label="服务日期" align="center" prop="serviceDate"/>
      <el-table-column label="项目区域" align="center" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion" />
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" width="250"/>
      <el-table-column label="项目状态" align="center" prop="contractStatus" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.contractStatus == 1" type="success">履约中</el-tag>
          <el-tag v-if="scope.row.contractStatus == 2" type="danger">已到期</el-tag>
          <el-tag v-if="scope.row.contractStatus == 3" type="info">已终止</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发票类型" align="center" prop="invoiceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_invoice_type" :value="scope.row.invoiceType" />
        </template>
      </el-table-column>
      <el-table-column label="送达方式" align="center" prop="serviceType">
        <template slot-scope="scope">
          <span v-if="scope.row.serviceType == 1">快递</span>
          <span v-if="scope.row.serviceType == 2">全电</span>
          <span v-if="scope.row.serviceType == 3">自送</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="快递单号" align="center" prop="courierNumber"/>-->
      <el-table-column label="发票号码" align="center" prop="invoiceNumber" width="200px;"/>
      <el-table-column label="应开票金额" align="center" prop="answerInvoiceAmount">
        <template slot-scope="scope">
          {{ toMoney(scope.row.answerInvoiceAmount) }}
        </template>
      </el-table-column>
      <el-table-column label="实开票金额" align="center" prop="realInvoiceAmount">
        <template slot-scope="scope">
          {{ toMoney(scope.row.realInvoiceAmount) }}
        </template>
      </el-table-column>
      <el-table-column label="差额" align="center">
        <template slot-scope="scope">
          {{ toMoney(scope.row.answerInvoiceAmount - scope.row.realInvoiceAmount) }}
        </template>
      </el-table-column>
      <el-table-column label="开票日期" align="center" prop="invoiceDate"/>
      <el-table-column label="签收状态" align="center" prop="signStatus">
        <template slot-scope="scope">
          {{ scope.row.signStatus == 0 ? '未签收' : '已签收' }}
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createName"/>
      <el-table-column label="附件" align="center" prop="invoiceAttrs">
        <template slot-scope="scope">
          <div v-if="scope.row.invoiceAttrs">
            <el-link style="margin-left:5px;margin-right:5px;" type="primary" v-for="(item,index) in scope.row.invoiceAttrs.split(',')" :href="item" :key="index">附件{{index+1}}</el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['cld:invoice:edit']">修改</el-button>
          <el-button v-if="scope.row.invoiceStatus == 0" size="mini" type="text" @click="handleDeduction(scope.row)"
                     v-hasPermi="['cld:invoiceDeduction:list']">扣款</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改发票管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-position="top" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="项目区域" prop="projectRegion">
              <el-select v-model="form.projectRegion" :disabled="true" placeholder="请选择">
                <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" :disabled="true" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="实开票金额" prop="realInvoiceAmount">
              <el-input type="number" v-model="form.realInvoiceAmount" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="开票时间" prop="invoiceDate">
              <el-date-picker clearable v-model="form.invoiceDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发票号码" prop="invoiceNumber">
              <el-input v-model="form.invoiceNumber" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="送达方式" prop="serviceType">
              <el-select v-model="form.serviceType" placeholder="请选择">
                <el-option v-for="item in serviceTypeOptions" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" v-if="form.serviceType == 1">
          <el-col :span="8">
            <el-form-item label="收件人" prop="courierUser">
              <el-input v-model="form.courierUser" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="快递公司" prop="courierName">
              <el-input v-model="form.courierName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="快递单号" prop="courierNumber">
              <el-input v-model="form.courierNumber" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="附件" prop="invoiceAttrs">
              <file-upload v-model="form.invoiceAttrs" @input="handleUploadFile" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 扣款对话框 -->
    <el-dialog :title="title" :visible.sync="deductionOpen" width="1000px" append-to-body>
      <deduction :invoiceId="form.invoiceId" v-if="deductionOpen"></deduction>
    </el-dialog>
  </div>
</template>

<script>
import { listInvoice, getInvoice, updateInvoice } from "@/api/cld/invoice";
import { invoiceStatusOptions, signStatusOptions, serviceTypeOptions } from '@/assets/staticDict'
import deduction from "./deduction.vue";
export default {
  name: "region",
  dicts: ['cld_project_region','cld_invoice_type'],
  components: {
    deduction:deduction,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票管理表格数据
      invoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      deductionOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectRegion: null,
        projectName: null,
        invoiceStatus: null,
        serviceDate: null,
        signStatus: null
      },
      // 开票状态
      invoiceStatusOptions: invoiceStatusOptions,
      // 签收状态
      signStatusOptions: signStatusOptions,
      // 送达方式
      serviceTypeOptions: serviceTypeOptions,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        realInvoiceAmount: [
          { required: true, message: "实开票金额不能为空", trigger: "blur" }
        ],
        invoiceDate: [
          { required: true, message: "开票时间不能为空", trigger: "change" }
        ],
        invoiceNumber: [
          { required: true, message: "发票号码不能为空", trigger: "blur" }
        ],
        serviceType: [
          { required: true, message: "请选择送达方式", trigger: "change" }
        ],
        courierUser: [
          { required: true, message: "收件人不能为空", trigger: "blur" }
        ],
        courierName: [
          { required: true, message: "快递公司不能为空", trigger: "blur" }
        ],
        courierNumber: [
          { required: true, message: "快递单号不能为空", trigger: "blur" }
        ],
        invoiceAttrs: [
          { required: true, message: "请上传附件", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询发票管理列表 */
    getList() {
      this.loading = true;
      listInvoice(this.queryParams).then(response => {
        this.invoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        invoiceId: null,
        projectId: null,
        serviceDate: null,
        serviceType: null,
        courierName: null,
        courierUser: null,
        courierNumber: null,
        invoiceNumber: null,
        answerInvoiceAmount: null,
        realInvoiceAmount: null,
        invoiceYear: null,
        invoiceMonth: null,
        invoiceDate: null,
        invoiceStatus: null,
        signStatus: null,
        invoiceAttrs: null,
        createId: null,
        createName: null,
        createTime: null,
        flag: null
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const invoiceId = row.invoiceId || this.ids
      getInvoice(invoiceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "发票信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateInvoice(this.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cld/invoice/export', {
        ...this.queryParams
      }, `发票管理_${new Date().getTime()}.xlsx`)
    },
    // 上传附件回调
    handleUploadFile(e) {
      this.form.invoiceAttrs = e;
    },
    /** 扣款操作 */
    handleDeduction(row) {
      this.deductionOpen = true;
      this.form.invoiceId = row.invoiceId;
      this.title = row.projectName+"-扣款信息";
    },
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
